﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Citas.Negocio;
using Citas.Dominio;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;

namespace Citas.Controllers
{
    public class ConsultaCitaController : Controller
    {
        IAdministracionService adminService = null;
        //
        // GET: /Combinacion/

        public ActionResult Index()
        {
            String perfil = (String)Session["perfil"];
            String usuario = (String)Session["usuario"];
            if (perfil == "Paciente")
            {
                ICollection<Cita> modelo = adminService.ListarCitaXNumeroODocumento(0, usuario);
                return View("Index", modelo);
            }
            else
            {
                return View(adminService.ListarCitas());
            }
        }

        public ActionResult Buscar(int numero = 0, string documento = "0")
        {
            if (numero <= 0) numero = 0;

            if (documento == null) documento = "0";

            ICollection<Cita> modelo = adminService.ListarCitaXNumeroODocumento(numero, documento);
            return View("Index", modelo);
        }

        public ActionResult Ver(int id)
        {
            try
            {
                //atención
                HttpWebRequest reqAtencion = (HttpWebRequest)WebRequest.Create("http://localhost:49928/AtencionesMedicas.svc/AtencionMedica/" + id.ToString());
                reqAtencion.Method = "GET";
                HttpWebResponse resAtencion = (HttpWebResponse)reqAtencion.GetResponse();
                StreamReader readerAtencion = new StreamReader(resAtencion.GetResponseStream());
                String atencionJson = readerAtencion.ReadToEnd();
                JavaScriptSerializer jsAtencion = new JavaScriptSerializer();
                AtencionMedica atencionObtenida = jsAtencion.Deserialize<AtencionMedica>(atencionJson);

                TempData["atencion_diagnostico"] = atencionObtenida.Diagnostico;

                // receta
                HttpWebRequest reqReceta = (HttpWebRequest)WebRequest.Create("http://localhost:49928/Recetas.svc/Receta/" + id.ToString());
                reqReceta.Method = "GET";
                HttpWebResponse resReceta = (HttpWebResponse)reqReceta.GetResponse();
                StreamReader readerReceta = new StreamReader(resReceta.GetResponseStream());
                String recetaJson = readerReceta.ReadToEnd();
                JavaScriptSerializer jsReceta = new JavaScriptSerializer();
                ICollection<Receta> recetaObtenida = jsReceta.Deserialize<ICollection<Receta>>(recetaJson);
                
                TempData["receta"] = recetaObtenida;
                
                return View(adminService.ObtenerCita(id));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View("Index", adminService.ListarCitas());
            }
        }
    }
}
